if not menu then menu = {} end

if not menu.label then


dofile (getScriptPath() .. "\\..\\lib\\lib.lua")


menu.label = {price = {},
	ini = lib.path.init .. "labels.ini",
	prices = {"tp", "lm", "sl", "tev"},
	codes = {}, --seccodes to prices table
	show = {"tp", "tev", "lm", "sl"},		-- show in table
	fmt = {tp = "%s [SET]", sl = "%s [SET]"},		-- format in table
	loaded = 0,
	changed = {}
}


processor:AddInit(function()
	for i, v in ipairs(menu.label.prices) do
		menu.label.prices[v] = {index = i}
	end
end)


processor:AddInit(function()
	window:AddRow()
	menu.label.rows = 0
	
	for i, v in ipairs(menu.label.show) do
		menu.label.show[v] = {index = i, row = window:AddRow()}
		
		if not menu.label.row then
			menu.label.row = menu.label.show[v].row
		end
		
		menu.label.rows = menu.label.rows + 1
		window:SetCell(menu.label.show[v].row, 1, string.upper(v))
	end
end)


processor:AddTimer(function()
	if menu.label.do_limit and not orders:InProcess() then
		local pos, limits = orders:GetPosition(menu.label.do_limit)
		
		if pos == 0 and lib:exists(lib.path.init .. "copy2order.flag") or limits ~= 0 then
			menu.action:Limit()
		end
		
		if pos ~= 0 and limits == 0 then
			menu.action:SetTake()
		end
		
		menu.label.do_limit = nil
	end
end)


processor:AddTimer(function()
	if orders:InProcess() then return end
	
	local labels, instrument = lib:LoadLabels()
	
	if labels and instrument and menu.label.loaded ~= labels.seconds_ then
		if menu.chart.instrument then
			menu.label.codes[menu.chart.instrument.code] = menu.label.price
		end
		
		menu.chart.changed.instrument = true
		menu.chart.changed.selected = true
		menu.chart.instrument = instrument
		window:SetCell(menu.chart.row, 1, instrument.code)
		menu.window.caption = instrument.short_name
		menu.label.loaded = labels.seconds_
		menu.label.do_limit = menu.chart.instrument.code
	else
		labels = nil
	end
	
	if labels then
		--message(lib:toline(labels))
		local found = {}
		local is_long
		
		if labels.sl and (labels.lm or labels.tp or labels.tev) then
			if labels.sl < (labels.lm or labels.tp or labels.tev) then
				is_long = true
			elseif labels.sl > (labels.lm or labels.tp or labels.tev) then
				is_long = false
			end
		end
		
		for _, v in ipairs(menu.label.prices) do
			if labels[v] then
				if is_long == nil or v == "lm" or v == "tev" then
					found[v] = lib:NormalizePrice(labels[v], menu.chart.instrument)
				else
					found[v] = lib:NormalizePrice(labels[v], menu.chart.instrument, not is_long)
				end
				
				if menu.label.price[v] ~= found[v] then
					if menu.label.price[v] and found[v] then
						menu.label.changed[v] = found[v] - menu.label.price[v]
					else
						menu.label.changed[v] = 0
					end
				end
			else
				menu.label.changed[v] = 0
			end
		end
		
		if found.lm and found.sl and lib:exists(lib.path.init .. "take_even.flag")
			and (not found.tev or found.lm > found.sl and found.tev <= found.lm or found.lm < found.sl and found.tev >= found.lm)
		then
			message("Menu load labels tev before set " .. tostring(found.tev) .. " " .. tostring(not found.tev) .. " " .. tostring(found.lm) .. " " .. tostring(found.sl))
			found.tev = orders:CalcTakeEvenPrice(found.lm, found.sl, menu.portfolio.commission)
			message("Menu load labels set tev " .. tostring(found.tev))
		end
								
		if found.lm and found.sl and lib:IsTest() then
			local lm = orders:GetShiftPrice(instrument, found.lm > found.sl)
			if lm then
				local sh = lm - found.lm
				found.lm = found.lm + sh
				found.sl = found.sl + sh
				if found.tp then
					found.tp = found.tp + sh
				end
			end
		end
		
		menu.label.price = found
		
		lib:SubscribeLevel2(instrument.code, instrument.class_code)
	end
end)


processor:AddTimer(function()
	if menu.label.price.lm and menu.label.price.sl then
		if menu.label.price.lm > menu.label.price.sl then
			menu.label.long = true
		else
			menu.label.long = false
		end
	else
		menu.label.long = nil
	end
end)


processor:AddTimer(function()
	if menu.label.price.tp
		and (menu.label.long == nil
			or not menu.label.price.lm
			or menu.label.long and menu.label.price.lm >= menu.label.price.tp
			or not menu.label.long and menu.label.price.lm <= menu.label.price.tp
		)
	then
		menu.label.price.tp = nil
	end
end)


processor:AddTimer(function()
	for i, v in ipairs(menu.label.prices) do
		if menu.label.changed[v] then
			if menu.label.show[v] then
				if menu.label.price[v] then
					window:SetCell(menu.label.show[v].row, 2, string.format(menu.label.fmt[v] or "%s", tostring(menu.label.price[v])))
				else
					window:SetCell(menu.label.show[v].row, 2, "")
				end
			end
		end
	end
end)


end -- of file
